package cn.fsd.controller;

import cn.fsd.domain.Transfer;
import cn.fsd.domain.Transfer_path;
import cn.fsd.query.dispatch.TransferQuery;
import cn.fsd.service.TransferService;
import cn.fsd.vo.PageBean;
import cn.fsd.vo.PointBean;
import cn.fsd.vo.ResultBean;
import cn.fsd.vo.storeroom.transfer.TransferCommodityTaskVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/transfer",produces = "application/json;charset=utf-8")
@Api(value = "库房调度的API",description = "库房调度相关的功能")
public class TransferController {

    @Autowired
    TransferService transferService;



//    @PostMapping("/{order_id}") //post + /transfer/order_id
//    @ApiOperation(value = "根据订单生成调拨单以及对应的1-3条调拨路径，*****没用了，直接OrderController的一键调度即可")
//    public ResultBean add(@PathVariable("order_id") String order_id){
//        String msg = transferService.add(order_id);
//        if("success".equals(msg)) return ResultBean.success();
//        return ResultBean.error();
//    }

    @PostMapping("/page") //post + /transfer/page
    @ApiOperation(value = "调拨单管理页面的分页查询")
    public ResultBean findByPage(@RequestBody TransferQuery transferQuery){
        PageBean<Transfer> pageBean = transferService.findByPage(transferQuery);
        return ResultBean.success(pageBean);
    }

    @GetMapping("/getStoreroom/{receiver_address}") //get + /transfer/getStoreroom/receiver_address
    @ApiOperation(value = "根据订单中的收货人地址确定中心库房，以进行缺货判断")
    public ResultBean<String> getStoreroom(@PathVariable("receiver_address") String receiver_address){
        //调用Service层的添加方法
        String result = transferService.getStoreroom(receiver_address);//中心库房id
        if(result!=null&&!result.equals("")) return ResultBean.success("success",result);
        return ResultBean.error();
    }

    @GetMapping("/point/{transfer_id}") //get + /transfer/point/transfer_id
    @ApiOperation(value = "根据调拨单id查询分站和库房的信息，用于在地图中显示标注")
    public ResultBean showPoint(@PathVariable("transfer_id") String transfer_id){
        List<PointBean> pointList = transferService.showPoint(transfer_id);
        if(pointList.size()==2) return ResultBean.success(pointList);
        return ResultBean.error();
    }

//    @GetMapping("/path/{transfer_id}") //get + /transfer/path/transfer_id
//    @ApiOperation(value = "根据调拨单id查询分站到库房的已选路径，用于查看详情页面在地图中显示路径，*****没用了，直接调用手动调拨的接口即可")
//    public ResultBean showPath(@PathVariable("transfer_id") String transfer_id){
//        Transfer_path path = transferService.showPath(transfer_id);
//        if(path!=null) return ResultBean.success(path);
//        return ResultBean.error();
//    }

    @GetMapping("/pathAll/{transfer_id}") //get + /transfer/pathAll/transfer_id
    @ApiOperation(value = "根据调拨单id查询分站到库房的所有路径，用于手动调拨页面在地图中显示路径")
    public ResultBean showPathAll(@PathVariable("transfer_id") String transfer_id){
        List<Transfer_path> paths = transferService.showPathAll(transfer_id);
        if(paths.size()>=1) return ResultBean.success(paths);
        return ResultBean.error();
    }

    @PutMapping("/{transfer_id}/{path_num}") //put + /transfer/transfer_id/path_num
    @ApiOperation(value = "调拨单手动调度页面的修改路径")
    public ResultBean update(@PathVariable("transfer_id") String transfer_id,@PathVariable("path_num") Integer path_num){
        String msg = transferService.update(transfer_id,path_num);
        if("success".equals(msg)) return ResultBean.success();
        return ResultBean.error();
    }

    @PostMapping("/{transfer_id}/{waypoints}") //post + /transfer/transfer_id/waypoints
    @ApiOperation(value = "手动调度时，根据设置的途径点生成1条新路径，并设置新路径为已选路径")
    public ResultBean addWithWaypoints(@PathVariable("transfer_id") String transfer_id,@PathVariable("waypoints") String waypoints){//途径点格式：纬度,经度。示例：40.056878,116.30815|40.063597,116.364973
        String msg = transferService.addWithWaypoints(transfer_id,waypoints);
        if("success".equals(msg)) return ResultBean.success();
        return ResultBean.error();
    }

    @PutMapping("/{transfer_id}") //put + /transfer/transfer_id
    @ApiOperation(value = "调拨单调度完成进行确认，订单状态修改为'已调度'")
    public ResultBean sure(@PathVariable("transfer_id") String transfer_id){
        String msg = transferService.sure(transfer_id);
        if("success".equals(msg)) return ResultBean.success();
        return ResultBean.error();
    }

    @GetMapping("/getPathChoice/{transfer_id}") //get + /transfer/getPathChoice/transfer_id
    @ApiOperation(value = "根据transfer_id查询所选路径")
    public ResultBean getPathChoice(@PathVariable("transfer_id") String transfer_id){
        Integer path_choice = transferService.getPathChoice(transfer_id);
        System.out.println(path_choice);
        return ResultBean.success(path_choice);
    }

    @PostMapping("/findByStoreroomByPage")
    @ApiOperation(value = "（StoreroomService调用）查询某仓库需要调拨入库的调拨单VO,调拨单状态为'已调度'")
    public PageInfo<TransferCommodityTaskVO> findByStoreroomByPage(@RequestBody TransferQuery query){
        return transferService.findByStoreroomByPage(query);
    }

    @PostMapping("/findByStationByPage")
    @ApiOperation(value = "（StoreroomService调用）查询某仓库需要调拨出库的调拨单VO,调拨单状态为'已出库'")
    PageInfo<TransferCommodityTaskVO> findByStationByPage(@RequestBody TransferQuery query){
        return transferService.findByStationByPage(query);
    }

    @PostMapping("/changeStatus")
    @ApiOperation(value = "（StoreroomService调用）修改某调拨单的状态")
    Boolean changeStatus(@RequestBody Transfer transfer){
        return transferService.changeStatus(transfer);
    }


    @GetMapping("getByOrderId/{order_id}")
    @ApiOperation(value = "（CustomerService调用）根据order_id查询调拨单")
    public Transfer findByOrderId(@PathVariable("order_id") String order_id){
        return transferService.findByOrderId(order_id);
    }

    @GetMapping("getById/{transfer_id}")
    @ApiOperation(value = "（CustomerService调用）根据id查询调拨单")
    public Transfer findById(@PathVariable("transfer_id") String order_id){
        return transferService.findById(order_id);
    }

}
